<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<pre>
<div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--><span style="color: #008080;">  1</span> <span style="color: #000000;">&lt;?</span><span style="color: #000000;">php
</span><span style="color: #008080;">  2</span> <span style="color: #000000;"></span><span style="color: #0000FF;">class</span><span style="color: #000000;"> Lang_Auto_Detect
</span><span style="color: #008080;">  3</span> <span style="color: #000000;">{
</span><span style="color: #008080;">  4</span> <span style="color: #000000;">    </span><span style="color: #008000;">//</span><span style="color: #008000;"> основные переменные
</span><span style="color: #008080;">  5</span> <span style="color: #008000;">    // список поддерживаемых языков</span><span style="color: #008000;">
</span><span style="color: #008080;">  6</span> <span style="color: #008000;"></span><span style="color: #000000;">    </span><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #800080;">$lang</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">Array</span><span style="color: #000000;">(</span><span style="color: #000000;">'</span><span style="color: #000000;">en</span><span style="color: #000000;">'</span><span style="color: #000000;">=&gt;</span><span style="color: #0000FF;">array</span><span style="color: #000000;">(</span><span style="color: #000000;">'</span><span style="color: #000000;">English</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">http://en.wikipedia.org/wiki/English_language</span><span style="color: #000000;">'</span><span style="color: #000000;">)</span><span style="color: #000000;">,</span><span style="color: #000000;">
</span><span style="color: #008080;">  7</span> <span style="color: #000000;">                         </span><span style="color: #000000;">'</span><span style="color: #000000;">ru</span><span style="color: #000000;">'</span><span style="color: #000000;">=&gt;</span><span style="color: #0000FF;">array</span><span style="color: #000000;">(</span><span style="color: #000000;">'</span><span style="color: #000000;">Russian</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">http://ru.wikipedia.org/wiki/%D0%A0%D1%83%D1%81%D1%81%D0%BA%D0%B8%D0%B9_%D1%8F%D0%B7%D1%8B%D0%BA</span><span style="color: #000000;">'</span><span style="color: #000000;">)</span><span style="color: #000000;">,</span><span style="color: #000000;">
</span><span style="color: #008080;">  8</span> <span style="color: #000000;">                         </span><span style="color: #000000;">'</span><span style="color: #000000;">ua</span><span style="color: #000000;">'</span><span style="color: #000000;">=&gt;</span><span style="color: #0000FF;">array</span><span style="color: #000000;">(</span><span style="color: #000000;">'</span><span style="color: #000000;">Ukraine</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">http://uk.wikipedia.org/wiki/%D0%A3%D0%BA%D1%80%D0%B0%D1%97%D0%BD%D1%81%D1%8C%D0%BA%D0%B0_%D0%BC%D0%BE%D0%B2%D0%B0</span><span style="color: #000000;">'</span><span style="color: #000000;">)
</span><span style="color: #008080;">  9</span> <span style="color: #000000;">                        );
</span><span style="color: #008080;"> 10</span> <span style="color: #000000;">    </span><span style="color: #008000;">//</span><span style="color: #008000;"> порог чувствительности, сколько в % должно быть символов языка, чтобы он был определён</span><span style="color: #008000;">
</span><span style="color: #008080;"> 11</span> <span style="color: #008000;"></span><span style="color: #000000;">    </span><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #800080;">$detect_range</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #000000;">75</span><span style="color: #000000;">; 
</span><span style="color: #008080;"> 12</span> <span style="color: #000000;">    </span><span style="color: #008000;">//</span><span style="color: #008000;"> обрабатывать ли многоязычные документы и возвращать массив используемых языков</span><span style="color: #008000;">
</span><span style="color: #008080;"> 13</span> <span style="color: #008000;"></span><span style="color: #000000;">    </span><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #800080;">$detect_multi_lang</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">false</span><span style="color: #000000;">; </span><span style="color: #008000;">//</span><span style="color: #008000;"> пока  не реализовано
</span><span style="color: #008080;"> 14</span> <span style="color: #008000;">    // возвращать все результаты и вероятности</span><span style="color: #008000;">
</span><span style="color: #008080;"> 15</span> <span style="color: #008000;"></span><span style="color: #000000;">    </span><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #800080;">$return_all_results</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">false</span><span style="color: #000000;">; </span><span style="color: #008000;">//</span><span style="color: #008000;"> в реальном применении лучше отключить
</span><span style="color: #008080;"> 16</span> <span style="color: #008000;">    // использовать дополнительно систему правил и исключений</span><span style="color: #008000;">
</span><span style="color: #008080;"> 17</span> <span style="color: #008000;"></span><span style="color: #000000;">    </span><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #800080;">$use_rules</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">false</span><span style="color: #000000;">; 
</span><span style="color: #008080;"> 18</span> <span style="color: #000000;">    </span><span style="color: #008000;">//</span><span style="color: #008000;">применять только правила (быстрее намного, но результат менее вероятен, чем больше текста, тем достовернее)</span><span style="color: #008000;">
</span><span style="color: #008080;"> 19</span> <span style="color: #008000;"></span><span style="color: #000000;">    </span><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #800080;">$use_rules_only</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">false</span><span style="color: #000000;">;
</span><span style="color: #008080;"> 20</span> <span style="color: #000000;">    </span><span style="color: #008000;">//</span><span style="color: #008000;"> приоритет правил над статистикой -</span><span style="color: #008000;">
</span><span style="color: #008080;"> 21</span> <span style="color: #008000;"></span><span style="color: #000000;">    </span><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #800080;">$use_rules_priory</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">true</span><span style="color: #000000;">; </span><span style="color: #008000;">//</span><span style="color: #008000;"> true - правила приоритетнее статистики, false - статистика перед правилами    
</span><span style="color: #008080;"> 22</span> <span style="color: #008000;">    // искать только первое правило или максимум совпадений?</span><span style="color: #008000;">
</span><span style="color: #008080;"> 23</span> <span style="color: #008000;"></span><span style="color: #000000;">    </span><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #800080;">$match_all_rules</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">false</span><span style="color: #000000;">; </span><span style="color: #008000;">//</span><span style="color: #008000;"> только одно иначе = все
</span><span style="color: #008080;"> 24</span> <span style="color: #008000;">    //использовать % от алфавита или общее количество символов каждого алфавита</span><span style="color: #008000;">
</span><span style="color: #008080;"> 25</span> <span style="color: #008000;"></span><span style="color: #000000;">    </span><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #800080;">$use_str_len_per_lang</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">true</span><span style="color: #000000;">; </span><span style="color: #008000;">//</span><span style="color: #008000;"> true - использовать общую длину текста приоритетнее, чем % от символов алфавита, false - наоборот
</span><span style="color: #008080;"> 26</span> <span style="color: #008000;">    
</span><span style="color: #008080;"> 27</span> <span style="color: #008000;">    // минимальная длина строки для детектирования</span><span style="color: #008000;">
</span><span style="color: #008080;"> 28</span> <span style="color: #008000;"></span><span style="color: #000000;">    </span><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #800080;">$min_str_len_detect</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #000000;">50</span><span style="color: #000000;">;
</span><span style="color: #008080;"> 29</span> <span style="color: #000000;">    </span><span style="color: #008000;">//</span><span style="color: #008000;"> для обеспечения нормальной производительности задайте максимальную длину в символах для сравнения</span><span style="color: #008000;">
</span><span style="color: #008080;"> 30</span> <span style="color: #008000;"></span><span style="color: #000000;">    </span><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #800080;">$max_str_len_detect</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #000000;">1680</span><span style="color: #000000;">; </span><span style="color: #008000;">//</span><span style="color: #008000;">
</span><span style="color: #008080;"> 31</span> <span style="color: #008000;">    
</span><span style="color: #008080;"> 32</span> <span style="color: #008000;">
</span><span style="color: #008080;"> 33</span> <span style="color: #008000;">    // внутренняя переменная - таблица алфавитов используемых при определении</span><span style="color: #008000;">
</span><span style="color: #008080;"> 34</span> <span style="color: #008000;"></span><span style="color: #000000;">    </span><span style="color: #0000FF;">private</span><span style="color: #000000;"> </span><span style="color: #800080;">$_langs</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">array</span><span style="color: #000000;">(
</span><span style="color: #008080;"> 35</span> <span style="color: #000000;">                    </span><span style="color: #000000;">'</span><span style="color: #000000;">en</span><span style="color: #000000;">'</span><span style="color: #000000;">=&gt;</span><span style="color: #0000FF;">array</span><span style="color: #000000;">(</span><span style="color: #000000;">'</span><span style="color: #000000;">a</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">b</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">c</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">d</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">e</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">f</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">g</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">h</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">i</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">j</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">k</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">l</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">m</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">n</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">o</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">p</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">q</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">r</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">s</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">t</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">u</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">v</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">w</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">x</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">y</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">z</span><span style="color: #000000;">'</span><span style="color: #000000;">)</span><span style="color: #000000;">,</span><span style="color: #000000;">
</span><span style="color: #008080;"> 36</span> <span style="color: #000000;">                    </span><span style="color: #000000;">'</span><span style="color: #000000;">ru</span><span style="color: #000000;">'</span><span style="color: #000000;">=&gt;</span><span style="color: #0000FF;">array</span><span style="color: #000000;">(</span><span style="color: #000000;">'</span><span style="color: #000000;">а</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">б</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">в</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">г</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">д</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">е</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">ё</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">ж</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">з</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">и</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">й</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">к</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">л</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">м</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">н</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">о</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">п</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">р</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">с</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">т</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">у</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">ф</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">х</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">ц</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">ч</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">ш</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">щ</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">ъ</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">ы</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">ь</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">э</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">ю</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">я</span><span style="color: #000000;">'</span><span style="color: #000000;">)</span><span style="color: #000000;">,</span><span style="color: #000000;">
</span><span style="color: #008080;"> 37</span> <span style="color: #000000;">                    </span><span style="color: #000000;">'</span><span style="color: #000000;">ua</span><span style="color: #000000;">'</span><span style="color: #000000;">=&gt;</span><span style="color: #0000FF;">array</span><span style="color: #000000;">(</span><span style="color: #000000;">'</span><span style="color: #000000;">а</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">б</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">в</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">г</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">ґ</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">д</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">е</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">є</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">ж</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">з</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">и</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">і</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">ї</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">й</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">к</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">л</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">м</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">н</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">о</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">п</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">р</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">с</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">т</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">у</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">ф</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">х</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">ц</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">ч</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">ш</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">щ</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">ь</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">ю</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;">'</span><span style="color: #000000;">я</span><span style="color: #000000;">'</span><span style="color: #000000;">)
</span><span style="color: #008080;"> 38</span> <span style="color: #000000;">                    );
</span><span style="color: #008080;"> 39</span> <span style="color: #000000;">    
</span><span style="color: #008080;"> 40</span> <span style="color: #000000;">    </span><span style="color: #008000;">//</span><span style="color: #008000;"> хранит правила
</span><span style="color: #008080;"> 41</span> <span style="color: #008000;">    // правила  это символы или строки, наличие которой (любой или всех)  автоматически влечёт идентификацию текста</span><span style="color: #008000;">
</span><span style="color: #008080;"> 42</span> <span style="color: #008000;"></span><span style="color: #000000;">    </span><span style="color: #0000FF;">private</span><span style="color: #000000;"> </span><span style="color: #800080;">$_lang_rules</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">array</span><span style="color: #000000;">(
</span><span style="color: #008080;"> 43</span> <span style="color: #000000;">                                    </span><span style="color: #000000;">'</span><span style="color: #000000;">en</span><span style="color: #000000;">'</span><span style="color: #000000;">=&gt;</span><span style="color: #0000FF;">array</span><span style="color: #000000;">(</span><span style="color: #000000;">'</span><span style="color: #000000;">th</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;"> </span><span style="color: #000000;">'</span><span style="color: #000000;">ir</span><span style="color: #000000;">'</span><span style="color: #000000;">)</span><span style="color: #000000;">,</span><span style="color: #000000;">
</span><span style="color: #008080;"> 44</span> <span style="color: #000000;">                                    </span><span style="color: #000000;">'</span><span style="color: #000000;">ru</span><span style="color: #000000;">'</span><span style="color: #000000;">=&gt;</span><span style="color: #0000FF;">array</span><span style="color: #000000;">(</span><span style="color: #000000;">'</span><span style="color: #000000;">ъ</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;"> </span><span style="color: #000000;">'</span><span style="color: #000000;">ё</span><span style="color: #000000;">'</span><span style="color: #000000;"> )</span><span style="color: #000000;">,</span><span style="color: #000000;">
</span><span style="color: #008080;"> 45</span> <span style="color: #000000;">                                    </span><span style="color: #000000;">'</span><span style="color: #000000;">ua</span><span style="color: #000000;">'</span><span style="color: #000000;">=&gt;</span><span style="color: #0000FF;">array</span><span style="color: #000000;">(</span><span style="color: #000000;">'</span><span style="color: #000000;">ї</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;"> </span><span style="color: #000000;">'</span><span style="color: #000000;">є</span><span style="color: #000000;">'</span><span style="color: #000000;">)
</span><span style="color: #008080;"> 46</span> <span style="color: #000000;">                                ); 
</span><span style="color: #008080;"> 47</span> <span style="color: #000000;">    
</span><span style="color: #008080;"> 48</span> <span style="color: #000000;">    
</span><span style="color: #008080;"> 49</span> <span style="color: #000000;">    </span><span style="color: #008000;">//</span><span style="color: #008000;"> конструктор класса</span><span style="color: #008000;">
</span><span style="color: #008080;"> 50</span> <span style="color: #008000;"></span><span style="color: #000000;">       </span><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">function</span><span style="color: #000000;"> __construct()
</span><span style="color: #008080;"> 51</span> <span style="color: #000000;">       {
</span><span style="color: #008080;"> 52</span> <span style="color: #000000;">        </span><span style="color: #0000FF;">return</span><span style="color: #000000;"> </span><span style="color: #0000FF;">true</span><span style="color: #000000;">;        
</span><span style="color: #008080;"> 53</span> <span style="color: #000000;">       }
</span><span style="color: #008080;"> 54</span> <span style="color: #000000;">    
</span><span style="color: #008080;"> 55</span> <span style="color: #000000;">    </span><span style="color: #008000;">//</span><span style="color: #008000;"> подготовка введённой строки для сравнения</span><span style="color: #008000;">
</span><span style="color: #008080;"> 56</span> <span style="color: #008000;"></span><span style="color: #000000;">    </span><span style="color: #0000FF;">private</span><span style="color: #000000;"> </span><span style="color: #0000FF;">function</span><span style="color: #000000;"> _prepare_str(</span><span style="color: #800080;">$tmp_str</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">null</span><span style="color: #000000;">)
</span><span style="color: #008080;"> 57</span> <span style="color: #000000;">    {
</span><span style="color: #008080;"> 58</span> <span style="color: #000000;">        </span><span style="color: #0000FF;">if</span><span style="color: #000000;"> (</span><span style="color: #800080;">$tmp_str</span><span style="color: #000000;"> </span><span style="color: #000000;">==</span><span style="color: #000000;"> </span><span style="color: #0000FF;">null</span><span style="color: #000000;">) </span><span style="color: #0000FF;">return</span><span style="color: #000000;"> </span><span style="color: #0000FF;">false</span><span style="color: #000000;">; </span><span style="color: #008000;">//</span><span style="color: #008000;"> если ничего не передали - выйти</span><span style="color: #008000;">
</span><span style="color: #008080;"> 59</span> <span style="color: #008000;"></span><span style="color: #000000;">        
</span><span style="color: #008080;"> 60</span> <span style="color: #000000;">        </span><span style="color: #800080;">$tmp_str</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #008080;">trim</span><span style="color: #000000;">(</span><span style="color: #800080;">$tmp_str</span><span style="color: #000000;">);
</span><span style="color: #008080;"> 61</span> <span style="color: #000000;">        </span><span style="color: #800080;">$tmp_encoding</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> mb_detect_encoding(</span><span style="color: #800080;">$tmp_str</span><span style="color: #000000;">);
</span><span style="color: #008080;"> 62</span> <span style="color: #000000;">        
</span><span style="color: #008080;"> 63</span> <span style="color: #000000;">        </span><span style="color: #0000FF;">if</span><span style="color: #000000;"> (mb_strlen(</span><span style="color: #800080;">$tmp_str</span><span style="color: #000000;">,</span><span style="color: #000000;"> </span><span style="color: #800080;">$tmp_encoding</span><span style="color: #000000;">) </span><span style="color: #000000;">&gt;</span><span style="color: #000000;"> </span><span style="color: #800080;">$this</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">max_str_len_detect)
</span><span style="color: #008080;"> 64</span> <span style="color: #000000;">        {
</span><span style="color: #008080;"> 65</span> <span style="color: #000000;">            </span><span style="color: #008000;">//</span><span style="color: #008000;">обрезать длину текста, для производительности</span><span style="color: #008000;">
</span><span style="color: #008080;"> 66</span> <span style="color: #008000;"></span><span style="color: #000000;">            </span><span style="color: #800080;">$tmp_str</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> mb_substr(</span><span style="color: #800080;">$tmp_str</span><span style="color: #000000;">,</span><span style="color: #000000;"> </span><span style="color: #000000;">0</span><span style="color: #000000;">,</span><span style="color: #000000;"> </span><span style="color: #800080;">$this</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">max_str_len_detect</span><span style="color: #000000;">,</span><span style="color: #000000;"> </span><span style="color: #800080;">$tmp_encoding</span><span style="color: #000000;">);
</span><span style="color: #008080;"> 67</span> <span style="color: #000000;">        }
</span><span style="color: #008080;"> 68</span> <span style="color: #000000;">        </span><span style="color: #0000FF;">else</span><span style="color: #000000;">
</span><span style="color: #008080;"> 69</span> <span style="color: #000000;">            </span><span style="color: #0000FF;">if</span><span style="color: #000000;"> (mb_strlen(</span><span style="color: #800080;">$tmp_str</span><span style="color: #000000;">,</span><span style="color: #000000;"> </span><span style="color: #800080;">$tmp_encoding</span><span style="color: #000000;">) </span><span style="color: #000000;">&lt;=</span><span style="color: #000000;"> </span><span style="color: #800080;">$this</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">min_str_len_detect) </span><span style="color: #0000FF;">return</span><span style="color: #000000;"> </span><span style="color: #0000FF;">false</span><span style="color: #000000;">;
</span><span style="color: #008080;"> 70</span> <span style="color: #000000;">        
</span><span style="color: #008080;"> 71</span> <span style="color: #000000;">        </span><span style="color: #008000;">//</span><span style="color: #008000;"> конвертируем кодировки</span><span style="color: #008000;">
</span><span style="color: #008080;"> 72</span> <span style="color: #008000;"></span><span style="color: #000000;">        </span><span style="color: #800080;">$tmp_str</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> mb_convert_encoding(</span><span style="color: #800080;">$tmp_str</span><span style="color: #000000;">,</span><span style="color: #000000;"> </span><span style="color: #000000;">'</span><span style="color: #000000;">UTF-8</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;"> </span><span style="color: #800080;">$tmp_encoding</span><span style="color: #000000;">);
</span><span style="color: #008080;"> 73</span> <span style="color: #000000;">        
</span><span style="color: #008080;"> 74</span> <span style="color: #000000;">        </span><span style="color: #008000;">//</span><span style="color: #008000;"> приводим все к нижнему регистру</span><span style="color: #008000;">
</span><span style="color: #008080;"> 75</span> <span style="color: #008000;"></span><span style="color: #000000;">        </span><span style="color: #800080;">$tmp_str</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> mb_strtolower(</span><span style="color: #800080;">$tmp_str</span><span style="color: #000000;">,</span><span style="color: #000000;"> </span><span style="color: #000000;">'</span><span style="color: #000000;">UTF-8</span><span style="color: #000000;">'</span><span style="color: #000000;">);
</span><span style="color: #008080;"> 76</span> <span style="color: #000000;">        
</span><span style="color: #008080;"> 77</span> <span style="color: #000000;">        </span><span style="color: #0000FF;">return</span><span style="color: #000000;"> </span><span style="color: #800080;">$tmp_str</span><span style="color: #000000;">;
</span><span style="color: #008080;"> 78</span> <span style="color: #000000;">    }
</span><span style="color: #008080;"> 79</span> <span style="color: #000000;">    
</span><span style="color: #008080;"> 80</span> <span style="color: #000000;">    </span><span style="color: #008000;">//</span><span style="color: #008000;"> функция определения языка по правилам
</span><span style="color: #008080;"> 81</span> <span style="color: #008000;">    // правила однозначно определяют язык, однако могут ошибаться :)</span><span style="color: #008000;">
</span><span style="color: #008080;"> 82</span> <span style="color: #008000;"></span><span style="color: #000000;">    </span><span style="color: #0000FF;">private</span><span style="color: #000000;"> </span><span style="color: #0000FF;">function</span><span style="color: #000000;"> _detect_from_rules(</span><span style="color: #800080;">$tmp_str</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">null</span><span style="color: #000000;">)
</span><span style="color: #008080;"> 83</span> <span style="color: #000000;">    {
</span><span style="color: #008080;"> 84</span> <span style="color: #000000;">        </span><span style="color: #0000FF;">if</span><span style="color: #000000;"> (</span><span style="color: #800080;">$tmp_str</span><span style="color: #000000;"> </span><span style="color: #000000;">==</span><span style="color: #000000;"> </span><span style="color: #0000FF;">null</span><span style="color: #000000;">) </span><span style="color: #0000FF;">return</span><span style="color: #000000;"> </span><span style="color: #0000FF;">false</span><span style="color: #000000;">; </span><span style="color: #008000;">//</span><span style="color: #008000;"> если ничего не передали - выйти</span><span style="color: #008000;">
</span><span style="color: #008080;"> 85</span> <span style="color: #008000;"></span><span style="color: #000000;">        </span><span style="color: #0000FF;">if</span><span style="color: #000000;"> (</span><span style="color: #000000;">!</span><span style="color: #008080;">is_array</span><span style="color: #000000;">(</span><span style="color: #800080;">$this</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">_lang_rules)) </span><span style="color: #0000FF;">return</span><span style="color: #000000;"> </span><span style="color: #0000FF;">false</span><span style="color: #000000;">;
</span><span style="color: #008080;"> 86</span> <span style="color: #000000;">        
</span><span style="color: #008080;"> 87</span> <span style="color: #000000;">        </span><span style="color: #008000;">//</span><span style="color: #008000;"> перебор всех правил</span><span style="color: #008000;">
</span><span style="color: #008080;"> 88</span> <span style="color: #008000;"></span><span style="color: #000000;">        </span><span style="color: #0000FF;">foreach</span><span style="color: #000000;"> (</span><span style="color: #800080;">$this</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">_lang_rules </span><span style="color: #0000FF;">as</span><span style="color: #000000;"> </span><span style="color: #800080;">$lang_code</span><span style="color: #000000;">=&gt;</span><span style="color: #800080;">$lang_rules</span><span style="color: #000000;">)
</span><span style="color: #008080;"> 89</span> <span style="color: #000000;">        {
</span><span style="color: #008080;"> 90</span> <span style="color: #000000;">            </span><span style="color: #800080;">$tmp_freq</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #000000;">0</span><span style="color: #000000;">;
</span><span style="color: #008080;"> 91</span> <span style="color: #000000;">            
</span><span style="color: #008080;"> 92</span> <span style="color: #000000;">            </span><span style="color: #0000FF;">foreach</span><span style="color: #000000;"> (</span><span style="color: #800080;">$lang_rules</span><span style="color: #000000;"> </span><span style="color: #0000FF;">as</span><span style="color: #000000;"> </span><span style="color: #800080;">$rule</span><span style="color: #000000;">)
</span><span style="color: #008080;"> 93</span> <span style="color: #000000;">            {
</span><span style="color: #008080;"> 94</span> <span style="color: #000000;">                </span><span style="color: #800080;">$tmp_term</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> mb_substr_count(</span><span style="color: #800080;">$tmp_str</span><span style="color: #000000;">,</span><span style="color: #000000;"> </span><span style="color: #800080;">$rule</span><span style="color: #000000;">);
</span><span style="color: #008080;"> 95</span> <span style="color: #000000;">
</span><span style="color: #008080;"> 96</span> <span style="color: #000000;">                </span><span style="color: #0000FF;">if</span><span style="color: #000000;"> (</span><span style="color: #800080;">$tmp_term</span><span style="color: #000000;"> </span><span style="color: #000000;">&gt;</span><span style="color: #000000;"> </span><span style="color: #000000;">1</span><span style="color: #000000;">) </span><span style="color: #008000;">//</span><span style="color: #008000;"> то есть символ в строе 1 или более раз</span><span style="color: #008000;">
</span><span style="color: #008080;"> 97</span> <span style="color: #008000;"></span><span style="color: #000000;">                {
</span><span style="color: #008080;"> 98</span> <span style="color: #000000;">                    </span><span style="color: #800080;">$tmp_freq</span><span style="color: #000000;">++</span><span style="color: #000000;">; </span><span style="color: #008000;">//</span><span style="color: #008000;"> увеличим счётчик символов языка, которые в этой строке есть</span><span style="color: #008000;">
</span><span style="color: #008080;"> 99</span> <span style="color: #008000;"></span><span style="color: #000000;">                }
</span><span style="color: #008080;">100</span> <span style="color: #000000;">                
</span><span style="color: #008080;">101</span> <span style="color: #000000;">                </span><span style="color: #008000;">//</span><span style="color: #008000;"> теперь проверим</span><span style="color: #008000;">
</span><span style="color: #008080;">102</span> <span style="color: #008000;"></span><span style="color: #000000;">                </span><span style="color: #0000FF;">if</span><span style="color: #000000;"> (</span><span style="color: #800080;">$this</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">match_all_rules </span><span style="color: #000000;">===</span><span style="color: #000000;"> </span><span style="color: #0000FF;">true</span><span style="color: #000000;">)
</span><span style="color: #008080;">103</span> <span style="color: #000000;">                {
</span><span style="color: #008080;">104</span> <span style="color: #000000;">                    </span><span style="color: #008000;">//</span><span style="color: #008000;"> нужно совпадение всех правил</span><span style="color: #008000;">
</span><span style="color: #008080;">105</span> <span style="color: #008000;"></span><span style="color: #000000;">                    </span><span style="color: #0000FF;">if</span><span style="color: #000000;"> (</span><span style="color: #800080;">$tmp_freq</span><span style="color: #000000;"> </span><span style="color: #000000;">==</span><span style="color: #000000;"> </span><span style="color: #008080;">count</span><span style="color: #000000;">(</span><span style="color: #800080;">$lang_rules</span><span style="color: #000000;">)) </span><span style="color: #0000FF;">return</span><span style="color: #000000;"> </span><span style="color: #800080;">$lang_code</span><span style="color: #000000;">;
</span><span style="color: #008080;">106</span> <span style="color: #000000;">                }
</span><span style="color: #008080;">107</span> <span style="color: #000000;">                </span><span style="color: #0000FF;">else</span><span style="color: #000000;">
</span><span style="color: #008080;">108</span> <span style="color: #000000;">                    {
</span><span style="color: #008080;">109</span> <span style="color: #000000;">                        </span><span style="color: #008000;">//</span><span style="color: #008000;"> достаточно одного</span><span style="color: #008000;">
</span><span style="color: #008080;">110</span> <span style="color: #008000;"></span><span style="color: #000000;">                        </span><span style="color: #0000FF;">if</span><span style="color: #000000;"> (</span><span style="color: #800080;">$tmp_freq</span><span style="color: #000000;"> </span><span style="color: #000000;">&gt;</span><span style="color: #000000;"> </span><span style="color: #000000;">0</span><span style="color: #000000;">) </span><span style="color: #0000FF;">return</span><span style="color: #000000;"> </span><span style="color: #800080;">$lang_code</span><span style="color: #000000;">;                    
</span><span style="color: #008080;">111</span> <span style="color: #000000;">                    }
</span><span style="color: #008080;">112</span> <span style="color: #000000;">            }
</span><span style="color: #008080;">113</span> <span style="color: #000000;">        }
</span><span style="color: #008080;">114</span> <span style="color: #000000;">    
</span><span style="color: #008080;">115</span> <span style="color: #000000;">        </span><span style="color: #0000FF;">return</span><span style="color: #000000;"> </span><span style="color: #0000FF;">false</span><span style="color: #000000;">;    
</span><span style="color: #008080;">116</span> <span style="color: #000000;">    }
</span><span style="color: #008080;">117</span> <span style="color: #000000;">
</span><span style="color: #008080;">118</span> <span style="color: #000000;">    </span><span style="color: #008000;">//</span><span style="color: #008000;"> функция определения языка по таблице</span><span style="color: #008000;">
</span><span style="color: #008080;">119</span> <span style="color: #008000;"></span><span style="color: #000000;">    </span><span style="color: #0000FF;">private</span><span style="color: #000000;"> </span><span style="color: #0000FF;">function</span><span style="color: #000000;"> _detect_from_tables(</span><span style="color: #800080;">$tmp_str</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">null</span><span style="color: #000000;">)
</span><span style="color: #008080;">120</span> <span style="color: #000000;">    {
</span><span style="color: #008080;">121</span> <span style="color: #000000;">        </span><span style="color: #0000FF;">if</span><span style="color: #000000;"> (</span><span style="color: #800080;">$tmp_str</span><span style="color: #000000;"> </span><span style="color: #000000;">==</span><span style="color: #000000;"> </span><span style="color: #0000FF;">null</span><span style="color: #000000;">) </span><span style="color: #0000FF;">return</span><span style="color: #000000;"> </span><span style="color: #0000FF;">false</span><span style="color: #000000;">; </span><span style="color: #008000;">//</span><span style="color: #008000;"> если ничего не передали - выйти
</span><span style="color: #008080;">122</span> <span style="color: #008000;">        
</span><span style="color: #008080;">123</span> <span style="color: #008000;">        //мы уже должны ранее обработать строку для сравнения        
</span><span style="color: #008080;">124</span> <span style="color: #008000;">        // перебираем все языки и для каждого определим вероятность</span><span style="color: #008000;">
</span><span style="color: #008080;">125</span> <span style="color: #008000;"></span><span style="color: #000000;">        </span><span style="color: #800080;">$lang_res</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">array</span><span style="color: #000000;">();
</span><span style="color: #008080;">126</span> <span style="color: #000000;">        
</span><span style="color: #008080;">127</span> <span style="color: #000000;">        </span><span style="color: #0000FF;">foreach</span><span style="color: #000000;"> (</span><span style="color: #800080;">$this</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">lang </span><span style="color: #0000FF;">as</span><span style="color: #000000;"> </span><span style="color: #800080;">$lang_code</span><span style="color: #000000;">=&gt;</span><span style="color: #800080;">$lang_name</span><span style="color: #000000;">)
</span><span style="color: #008080;">128</span> <span style="color: #000000;">        {
</span><span style="color: #008080;">129</span> <span style="color: #000000;">            </span><span style="color: #800080;">$lang_res</span><span style="color: #000000;">[</span><span style="color: #800080;">$lang_code</span><span style="color: #000000;">] </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #000000;">0</span><span style="color: #000000;">; </span><span style="color: #008000;">//</span><span style="color: #008000;">по умолчанию 0, то есть не этот язык</span><span style="color: #008000;">
</span><span style="color: #008080;">130</span> <span style="color: #008000;"></span><span style="color: #000000;">            
</span><span style="color: #008080;">131</span> <span style="color: #000000;">            </span><span style="color: #800080;">$tmp_freq</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #000000;">0</span><span style="color: #000000;">; </span><span style="color: #008000;">//</span><span style="color: #008000;"> частота символов текущего языка</span><span style="color: #008000;">
</span><span style="color: #008080;">132</span> <span style="color: #008000;"></span><span style="color: #000000;">            </span><span style="color: #800080;">$full_lang_symbols</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #000000;">0</span><span style="color: #000000;">; </span><span style="color: #008000;">//</span><span style="color: #008000;">полное количество символов этого языка
</span><span style="color: #008080;">133</span> <span style="color: #008000;">            
</span><span style="color: #008080;">134</span> <span style="color: #008000;">            // так как длина строки может быть произвольной, а алфавит одинаковый, то цикл по алфавитам</span><span style="color: #008000;">
</span><span style="color: #008080;">135</span> <span style="color: #008000;"></span><span style="color: #000000;">            </span><span style="color: #800080;">$cur_lang</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">$this</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">_langs[</span><span style="color: #800080;">$lang_code</span><span style="color: #000000;">];
</span><span style="color: #008080;">136</span> <span style="color: #000000;">                        
</span><span style="color: #008080;">137</span> <span style="color: #000000;">            </span><span style="color: #0000FF;">foreach</span><span style="color: #000000;"> (</span><span style="color: #800080;">$cur_lang</span><span style="color: #000000;"> </span><span style="color: #0000FF;">as</span><span style="color: #000000;"> </span><span style="color: #800080;">$l_item</span><span style="color: #000000;">)
</span><span style="color: #008080;">138</span> <span style="color: #000000;">            {
</span><span style="color: #008080;">139</span> <span style="color: #000000;">                </span><span style="color: #008000;">//</span><span style="color: #008000;"> теперь посмотреть количество вхождений символа в строку</span><span style="color: #008000;">
</span><span style="color: #008080;">140</span> <span style="color: #008000;"></span><span style="color: #000000;">                </span><span style="color: #800080;">$tmp_term</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> mb_substr_count(</span><span style="color: #800080;">$tmp_str</span><span style="color: #000000;">,</span><span style="color: #000000;"> </span><span style="color: #800080;">$l_item</span><span style="color: #000000;">);
</span><span style="color: #008080;">141</span> <span style="color: #000000;">                
</span><span style="color: #008080;">142</span> <span style="color: #000000;">                </span><span style="color: #0000FF;">if</span><span style="color: #000000;"> (</span><span style="color: #800080;">$tmp_term</span><span style="color: #000000;"> </span><span style="color: #000000;">&gt;</span><span style="color: #000000;"> </span><span style="color: #000000;">1</span><span style="color: #000000;">) </span><span style="color: #008000;">//</span><span style="color: #008000;"> то есть символ в строе 1 или более раз</span><span style="color: #008000;">
</span><span style="color: #008080;">143</span> <span style="color: #008000;"></span><span style="color: #000000;">                {
</span><span style="color: #008080;">144</span> <span style="color: #000000;">                    </span><span style="color: #800080;">$tmp_freq</span><span style="color: #000000;">++</span><span style="color: #000000;">; </span><span style="color: #008000;">//</span><span style="color: #008000;"> увеличим счетчик символов языка, которые в этой строке есть</span><span style="color: #008000;">
</span><span style="color: #008080;">145</span> <span style="color: #008000;"></span><span style="color: #000000;">                    </span><span style="color: #800080;">$full_lang_symbols</span><span style="color: #000000;"> </span><span style="color: #000000;">+=</span><span style="color: #000000;"> </span><span style="color: #800080;">$tmp_term</span><span style="color: #000000;">;
</span><span style="color: #008080;">146</span> <span style="color: #000000;">                }
</span><span style="color: #008080;">147</span> <span style="color: #000000;">            }
</span><span style="color: #008080;">148</span> <span style="color: #000000;">
</span><span style="color: #008080;">149</span> <span style="color: #000000;">            </span><span style="color: #0000FF;">if</span><span style="color: #000000;"> (</span><span style="color: #800080;">$this</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">use_str_len_per_lang </span><span style="color: #000000;">===</span><span style="color: #000000;"> </span><span style="color: #0000FF;">true</span><span style="color: #000000;">)
</span><span style="color: #008080;">150</span> <span style="color: #000000;">            {
</span><span style="color: #008080;">151</span> <span style="color: #000000;">                </span><span style="color: #008000;">//</span><span style="color: #008000;">использовать общее количество символов</span><span style="color: #008000;">
</span><span style="color: #008080;">152</span> <span style="color: #008000;"></span><span style="color: #000000;">                </span><span style="color: #800080;">$lang_res</span><span style="color: #000000;">[</span><span style="color: #800080;">$lang_code</span><span style="color: #000000;">] </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">$full_lang_symbols</span><span style="color: #000000;">;
</span><span style="color: #008080;">153</span> <span style="color: #000000;">            }
</span><span style="color: #008080;">154</span> <span style="color: #000000;">            </span><span style="color: #0000FF;">else</span><span style="color: #000000;">
</span><span style="color: #008080;">155</span> <span style="color: #000000;">                </span><span style="color: #008000;">//</span><span style="color: #008000;"> Вычислить процент от всех символов алфавита</span><span style="color: #008000;">
</span><span style="color: #008080;">156</span> <span style="color: #008000;"></span><span style="color: #000000;">                </span><span style="color: #800080;">$lang_res</span><span style="color: #000000;">[</span><span style="color: #800080;">$lang_code</span><span style="color: #000000;">] </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #008080;">ceil</span><span style="color: #000000;">((</span><span style="color: #000000;">100</span><span style="color: #000000;"> </span><span style="color: #000000;">/</span><span style="color: #000000;"> </span><span style="color: #008080;">count</span><span style="color: #000000;">(</span><span style="color: #800080;">$cur_lang</span><span style="color: #000000;">) ) </span><span style="color: #000000;">*</span><span style="color: #000000;"> </span><span style="color: #800080;">$tmp_freq</span><span style="color: #000000;">);
</span><span style="color: #008080;">157</span> <span style="color: #000000;">            
</span><span style="color: #008080;">158</span> <span style="color: #000000;">        }
</span><span style="color: #008080;">159</span> <span style="color: #000000;">        
</span><span style="color: #008080;">160</span> <span style="color: #000000;">        </span><span style="color: #008000;">//</span><span style="color: #008000;"> так, теперь посомтрим что вышло</span><span style="color: #008000;">
</span><span style="color: #008080;">161</span> <span style="color: #008000;"></span><span style="color: #000000;">        </span><span style="color: #008080;">arsort</span><span style="color: #000000;">(</span><span style="color: #800080;">$lang_res</span><span style="color: #000000;">,</span><span style="color: #000000;"> SORT_NUMERIC); </span><span style="color: #008000;">//</span><span style="color: #008000;">сортируем массив первый элемент язык с большей вероятностью</span><span style="color: #008000;">
</span><span style="color: #008080;">162</span> <span style="color: #008000;"></span><span style="color: #000000;">        
</span><span style="color: #008080;">163</span> <span style="color: #000000;">        </span><span style="color: #0000FF;">if</span><span style="color: #000000;"> (</span><span style="color: #800080;">$this</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">return_all_results </span><span style="color: #000000;">==</span><span style="color: #000000;"> </span><span style="color: #0000FF;">true</span><span style="color: #000000;">)
</span><span style="color: #008080;">164</span> <span style="color: #000000;">        {
</span><span style="color: #008080;">165</span> <span style="color: #000000;">            </span><span style="color: #0000FF;">return</span><span style="color: #000000;"> </span><span style="color: #800080;">$lang_res</span><span style="color: #000000;">; </span><span style="color: #008000;">//</span><span style="color: #008000;"> если вернуть все результаты - возвращаем, иначе выбрать лучший</span><span style="color: #008000;">
</span><span style="color: #008080;">166</span> <span style="color: #008000;"></span><span style="color: #000000;">        }
</span><span style="color: #008080;">167</span> <span style="color: #000000;">        </span><span style="color: #0000FF;">else</span><span style="color: #000000;">
</span><span style="color: #008080;">168</span> <span style="color: #000000;">            {
</span><span style="color: #008080;">169</span> <span style="color: #000000;">                </span><span style="color: #008000;">//</span><span style="color: #008000;"> если больше указанного нами порога, возвратить код языка, иначе - null (то есть, мы не можем определить код языка)</span><span style="color: #008000;">
</span><span style="color: #008080;">170</span> <span style="color: #008000;"></span><span style="color: #000000;">                </span><span style="color: #800080;">$key</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #008080;">key</span><span style="color: #000000;">(</span><span style="color: #800080;">$lang_res</span><span style="color: #000000;">);
</span><span style="color: #008080;">171</span> <span style="color: #000000;">                
</span><span style="color: #008080;">172</span> <span style="color: #000000;">                </span><span style="color: #0000FF;">if</span><span style="color: #000000;"> (</span><span style="color: #800080;">$lang_res</span><span style="color: #000000;">[</span><span style="color: #800080;">$key</span><span style="color: #000000;">] </span><span style="color: #000000;">&gt;=</span><span style="color: #000000;"> </span><span style="color: #800080;">$this</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">detect_range)
</span><span style="color: #008080;">173</span> <span style="color: #000000;">                    </span><span style="color: #0000FF;">return</span><span style="color: #000000;"> </span><span style="color: #800080;">$key</span><span style="color: #000000;">;
</span><span style="color: #008080;">174</span> <span style="color: #000000;">                </span><span style="color: #0000FF;">else</span><span style="color: #000000;">
</span><span style="color: #008080;">175</span> <span style="color: #000000;">                    </span><span style="color: #0000FF;">return</span><span style="color: #000000;"> </span><span style="color: #0000FF;">null</span><span style="color: #000000;">;
</span><span style="color: #008080;">176</span> <span style="color: #000000;">            }
</span><span style="color: #008080;">177</span> <span style="color: #000000;">        
</span><span style="color: #008080;">178</span> <span style="color: #000000;">    }
</span><span style="color: #008080;">179</span> <span style="color: #000000;">
</span><span style="color: #008080;">180</span> <span style="color: #000000;">
</span><span style="color: #008080;">181</span> <span style="color: #000000;">    </span><span style="color: #008000;">//</span><span style="color: #008000;"> общая функция для определения языка</span><span style="color: #008000;">
</span><span style="color: #008080;">182</span> <span style="color: #008000;"></span><span style="color: #000000;">    </span><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">function</span><span style="color: #000000;"> lang_detect(</span><span style="color: #800080;">$tmp_str</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">null</span><span style="color: #000000;">)
</span><span style="color: #008080;">183</span> <span style="color: #000000;">    {
</span><span style="color: #008080;">184</span> <span style="color: #000000;">        </span><span style="color: #0000FF;">if</span><span style="color: #000000;"> (</span><span style="color: #800080;">$tmp_str</span><span style="color: #000000;"> </span><span style="color: #000000;">==</span><span style="color: #000000;"> </span><span style="color: #0000FF;">null</span><span style="color: #000000;">) </span><span style="color: #0000FF;">return</span><span style="color: #000000;"> </span><span style="color: #0000FF;">false</span><span style="color: #000000;">; </span><span style="color: #008000;">//</span><span style="color: #008000;"> если ничего не передали - выйти</span><span style="color: #008000;">
</span><span style="color: #008080;">185</span> <span style="color: #008000;"></span><span style="color: #000000;">        
</span><span style="color: #008080;">186</span> <span style="color: #000000;">        </span><span style="color: #800080;">$tmp_str</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">$this</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">_prepare_str(</span><span style="color: #800080;">$tmp_str</span><span style="color: #000000;">);
</span><span style="color: #008080;">187</span> <span style="color: #000000;">        
</span><span style="color: #008080;">188</span> <span style="color: #000000;">        </span><span style="color: #0000FF;">if</span><span style="color: #000000;"> (</span><span style="color: #800080;">$tmp_str</span><span style="color: #000000;"> </span><span style="color: #000000;">===</span><span style="color: #000000;"> </span><span style="color: #0000FF;">false</span><span style="color: #000000;">) </span><span style="color: #0000FF;">return</span><span style="color: #000000;"> </span><span style="color: #0000FF;">false</span><span style="color: #000000;">;
</span><span style="color: #008080;">189</span> <span style="color: #000000;">        
</span><span style="color: #008080;">190</span> <span style="color: #000000;">        </span><span style="color: #008000;">//</span><span style="color: #008000;"> если правила применяем ДО таблицы</span><span style="color: #008000;">
</span><span style="color: #008080;">191</span> <span style="color: #008000;"></span><span style="color: #000000;">        </span><span style="color: #0000FF;">if</span><span style="color: #000000;"> (</span><span style="color: #800080;">$this</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">use_rules_only </span><span style="color: #000000;">===</span><span style="color: #000000;"> </span><span style="color: #0000FF;">true</span><span style="color: #000000;">)
</span><span style="color: #008080;">192</span> <span style="color: #000000;">        {
</span><span style="color: #008080;">193</span> <span style="color: #000000;">            </span><span style="color: #800080;">$res</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">$this</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">_detect_from_rules(</span><span style="color: #800080;">$tmp_str</span><span style="color: #000000;">);
</span><span style="color: #008080;">194</span> <span style="color: #000000;">            
</span><span style="color: #008080;">195</span> <span style="color: #000000;">            </span><span style="color: #0000FF;">return</span><span style="color: #000000;"> </span><span style="color: #0000FF;">array</span><span style="color: #000000;">(</span><span style="color: #800080;">$res</span><span style="color: #000000;">,</span><span style="color: #000000;"> </span><span style="color: #800080;">$this</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">lang[</span><span style="color: #800080;">$res</span><span style="color: #000000;">]);
</span><span style="color: #008080;">196</span> <span style="color: #000000;">        }
</span><span style="color: #008080;">197</span> <span style="color: #000000;">        </span><span style="color: #0000FF;">else</span><span style="color: #000000;">
</span><span style="color: #008080;">198</span> <span style="color: #000000;">            {
</span><span style="color: #008080;">199</span> <span style="color: #000000;">                </span><span style="color: #008000;">//</span><span style="color: #008000;"> при использовании таблиц мы не можем получить полную раскладку по результатам, потому отключаем</span><span style="color: #008000;">
</span><span style="color: #008080;">200</span> <span style="color: #008000;"></span><span style="color: #000000;">                </span><span style="color: #800080;">$this</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">return_all_results </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">false</span><span style="color: #000000;">;
</span><span style="color: #008080;">201</span> <span style="color: #000000;">                
</span><span style="color: #008080;">202</span> <span style="color: #000000;">                </span><span style="color: #800080;">$res</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">$this</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">_detect_from_tables(</span><span style="color: #800080;">$tmp_str</span><span style="color: #000000;">);
</span><span style="color: #008080;">203</span> <span style="color: #000000;">        
</span><span style="color: #008080;">204</span> <span style="color: #000000;">                </span><span style="color: #0000FF;">if</span><span style="color: #000000;"> (</span><span style="color: #800080;">$tmp_str</span><span style="color: #000000;"> </span><span style="color: #000000;">===</span><span style="color: #000000;"> </span><span style="color: #0000FF;">false</span><span style="color: #000000;">) </span><span style="color: #0000FF;">return</span><span style="color: #000000;"> </span><span style="color: #0000FF;">false</span><span style="color: #000000;">;
</span><span style="color: #008080;">205</span> <span style="color: #000000;">                
</span><span style="color: #008080;">206</span> <span style="color: #000000;">                </span><span style="color: #0000FF;">if</span><span style="color: #000000;"> (</span><span style="color: #800080;">$this</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">use_rules </span><span style="color: #000000;">===</span><span style="color: #000000;"> </span><span style="color: #0000FF;">true</span><span style="color: #000000;">)
</span><span style="color: #008080;">207</span> <span style="color: #000000;">                {
</span><span style="color: #008080;">208</span> <span style="color: #000000;">                    </span><span style="color: #800080;">$res_rules</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">$this</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">_detect_from_rules(</span><span style="color: #800080;">$tmp_str</span><span style="color: #000000;">);
</span><span style="color: #008080;">209</span> <span style="color: #000000;">                    
</span><span style="color: #008080;">210</span> <span style="color: #000000;">                    </span><span style="color: #008000;">//</span><span style="color: #008000;"> исходим из настроек приоритета правил и статистики</span><span style="color: #008000;">
</span><span style="color: #008080;">211</span> <span style="color: #008000;"></span><span style="color: #000000;">                    </span><span style="color: #0000FF;">if</span><span style="color: #000000;"> (</span><span style="color: #800080;">$this</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">use_rules_priory </span><span style="color: #000000;">===</span><span style="color: #000000;"> </span><span style="color: #0000FF;">true</span><span style="color: #000000;">)
</span><span style="color: #008080;">212</span> <span style="color: #000000;">                    {
</span><span style="color: #008080;">213</span> <span style="color: #000000;">                        </span><span style="color: #008000;">//</span><span style="color: #008000;">правила имеют бОльший вес, чем статистика</span><span style="color: #008000;">
</span><span style="color: #008080;">214</span> <span style="color: #008000;"></span><span style="color: #000000;">                        </span><span style="color: #0000FF;">return</span><span style="color: #000000;">  </span><span style="color: #0000FF;">array</span><span style="color: #000000;">(</span><span style="color: #800080;">$res_rules</span><span style="color: #000000;">,</span><span style="color: #000000;"> </span><span style="color: #800080;">$this</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">lang[</span><span style="color: #800080;">$res_rules</span><span style="color: #000000;">]);
</span><span style="color: #008080;">215</span> <span style="color: #000000;">                    }
</span><span style="color: #008080;">216</span> <span style="color: #000000;">                    </span><span style="color: #0000FF;">else</span><span style="color: #000000;">
</span><span style="color: #008080;">217</span> <span style="color: #000000;">                        {
</span><span style="color: #008080;">218</span> <span style="color: #000000;">                            </span><span style="color: #0000FF;">return</span><span style="color: #000000;"> </span><span style="color: #0000FF;">array</span><span style="color: #000000;">(</span><span style="color: #800080;">$res</span><span style="color: #000000;">,</span><span style="color: #000000;"> </span><span style="color: #800080;">$this</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">lang[</span><span style="color: #800080;">$res</span><span style="color: #000000;">]);
</span><span style="color: #008080;">219</span> <span style="color: #000000;">                        }
</span><span style="color: #008080;">220</span> <span style="color: #000000;">                }
</span><span style="color: #008080;">221</span> <span style="color: #000000;">                </span><span style="color: #0000FF;">else</span><span style="color: #000000;">
</span><span style="color: #008080;">222</span> <span style="color: #000000;">                    </span><span style="color: #0000FF;">return</span><span style="color: #000000;"> </span><span style="color: #0000FF;">array</span><span style="color: #000000;">(</span><span style="color: #800080;">$res</span><span style="color: #000000;">,</span><span style="color: #000000;"> </span><span style="color: #800080;">$this</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">lang[</span><span style="color: #800080;">$res</span><span style="color: #000000;">]);
</span><span style="color: #008080;">223</span> <span style="color: #000000;">            }    
</span><span style="color: #008080;">224</span> <span style="color: #000000;">    }
</span><span style="color: #008080;">225</span> <span style="color: #000000;">}
</span><span style="color: #008080;">226</span> <span style="color: #000000;"></span><span style="color: #000000;">?&gt;</span></div>
</pre>

</body>
</html>
